/**
 * 
 */
package com.ruyipu.forum.model;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.ruyipu.forum.base.Constants;
import com.ruyipu.forum.base.Factory;
import com.ruyipu.forum.base.MyLog;
import com.ruyipu.forum.interfaces.ICategoryModel;
import com.ruyipu.forum.interfaces.IItem;
import com.ruyipu.forum.item.Category;

/**
 * @author donald
 * 
 */
public class CategoryModel implements ICategoryModel{
	
	public static final String TAG = CategoryModel.class.getSimpleName();
	public static final String NAME = CategoryModel.class.getName();
	
	
	public static final String CATEGORIES_URL = Constants.API_URL + "categories/";
	public static final String ALL_CATEGORIES_URL = CATEGORIES_URL + "all";

	
	public static final String ALL_DISCUSSIONS_URL = Constants.API_URL + "discussions/index";
	
	private static CategoryModel categoryModel;
	
	private CategoryModel() {
	}
	
	public static CategoryModel getInstance() {
		if(null == categoryModel) {
			categoryModel = new CategoryModel();
		}
		return categoryModel;
	}

	public List<IItem> getCategoriesFrom(JSONObject jsonObject) {
		List<IItem> dataList = new ArrayList<IItem>();
		if( null != jsonObject) {
			dataList = Factory.factoryItemList(jsonObject, Category.NAME);
		}
		return dataList;
	}

	public JSONObject getAllCategory(Context context){
		try {
			return Model.get(context, ALL_CATEGORIES_URL);
		} catch (UnsupportedEncodingException e) {
			MyLog.e(TAG, e);
		} catch (ClientProtocolException e) {
			MyLog.e(TAG, e);
		} catch (IOException e) {
			MyLog.e(TAG, e);
		} catch (JSONException e) {
			MyLog.e(TAG, e);
		}
		return null;
	}

	public JSONObject loadLocalAllCategory() {
		try {
			return Model.loadLocalData(ALL_CATEGORIES_URL);
		} catch (JSONException e) {
			MyLog.e(TAG, e);
			return null;
		}
	}

}
